﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GLYFrom.DiCi
{
    public partial class GoWuChe : Form
    {
        public GoWuChe()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Name = this.text_Name.Text.Trim();
            //用户Id
            int id = GeRenXX.Id;
            //订单状态
            int ZT = Convert.ToInt32(MeiJu.JR);
            DataTable table = DBHelper.select(string.Format("select * from T_DingDan where CPName like '%{0}%' and YHId='{1}' and DDZT='{2}'", Name,id,ZT));
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = table;
        }

        private void GoWuChe_Load(object sender, EventArgs e)
        {
            //给dataGridView加入数据
            this.dataGridView1.AutoGenerateColumns = false;
            int ZT=Convert.ToInt32(MeiJu.JR);
            DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT='{1}'", GeRenXX.Id,ZT));
            this.dataGridView1.DataSource = table;
            //显示余额
            DataTable Table = DBHelper.select(string.Format("select * from T_YongHu where YHId='{0}'", GeRenXX.Id));
            int money = Convert.ToInt32(Table.Rows[0]["YuE"]);
            this.label6.Text = money.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count>0)
            {
                //用户Id
                int Id = GeRenXX.Id;
                //订单状态
                int ZT = Convert.ToInt32(MeiJu.XD);
                //价格
                int Money = 0;


                for (int i = 0; i <this.dataGridView1.SelectedRows.Count; i++)
                {
                    //价格
                    Money += Convert.ToInt32(this.dataGridView1.SelectedRows[i].Cells["Col_JG"].Value);
                }
                
                //余额是否充足
                DataTable TAble = DBHelper.select(string.Format("select * from T_YongHu where YHId={0}", Id));
                int YuE = Convert.ToInt32(TAble.Rows[0]["YuE"]);
                if (YuE < Money)
                {
                    MessageBox.Show("余额不足,下单失败...");
                    return;
                }
                int sr = 0;
                for (int i = 0; i <this.dataGridView1.SelectedRows.Count; i++)
                {
                    //商品名称
                    string Name = this.dataGridView1.SelectedRows[i].Cells["Col_Name"].Value.ToString();
                    sr = DBHelper.ZSG(string.Format("update T_DingDan set DDZT='{0}' where YHId='{1}' and CPName='{2}'", ZT, Id, Name));
                }
                
                if (sr > 0)
                {
                    MessageBox.Show("下单成功！");
                    //扣钱
                    DataTable TABle = DBHelper.select(string.Format("select * from T_YongHu where YHId={0}",Id));
                    int Yue = Convert.ToInt32(TABle.Rows[0]["YuE"])- Money;
                    int sc = DBHelper.ZSG(string.Format("update T_YongHu set YuE='{0}' where YHid='{1}'", Yue, Id));
                    //刷新余额
                    DataTable Table = DBHelper.select(string.Format("select * from T_YongHu where YHId='{0}'", GeRenXX.Id));
                    int money = Convert.ToInt32(Table.Rows[0]["YuE"]);
                    this.label6.Text = money.ToString();
                    //刷新表
                    int zt = Convert.ToInt32(MeiJu.JR);
                    DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT='{1}'", GeRenXX.Id, zt));
                    this.dataGridView1.DataSource = null;
                    this.dataGridView1.DataSource = table;
                }
                else
                {
                    MessageBox.Show("下单失败！");
                }

            }
            else
            {
                MessageBox.Show("结算失败,未选中...");
            }
            
        }

        private void 结算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                //用户Id
                int Id = GeRenXX.Id;
                //订单状态
                int ZT = Convert.ToInt32(MeiJu.XD);
                //价格
                int Money = 0;


                for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
                {
                    //价格
                    Money += Convert.ToInt32(this.dataGridView1.SelectedRows[i].Cells["Col_JG"].Value);
                }

                //余额是否充足
                DataTable TAble = DBHelper.select(string.Format("select * from T_YongHu where YHId={0}", Id));
                int YuE = Convert.ToInt32(TAble.Rows[0]["YuE"]);
                if (YuE < Money)
                {
                    MessageBox.Show("余额不足,下单失败...");
                    return;
                }
                int sr = 0;
                for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
                {
                    //商品名称
                    string Name = this.dataGridView1.SelectedRows[i].Cells["Col_Name"].Value.ToString();
                    sr = DBHelper.ZSG(string.Format("update T_DingDan set DDZT='{0}' where YHId='{1}' and CPName='{2}'", ZT, Id, Name));
                }

                if (sr > 0)
                {
                    MessageBox.Show("下单成功！");
                    //扣钱
                    DataTable TABle = DBHelper.select(string.Format("select * from T_YongHu where YHId={0}", Id));
                    int Yue = Convert.ToInt32(TABle.Rows[0]["YuE"]) - Money;
                    int sc = DBHelper.ZSG(string.Format("update T_YongHu set YuE='{0}' where YHid='{1}'", Yue, Id));
                    //刷新余额
                    DataTable Table = DBHelper.select(string.Format("select * from T_YongHu where YHId='{0}'", GeRenXX.Id));
                    int money = Convert.ToInt32(Table.Rows[0]["YuE"]);
                    this.label6.Text = money.ToString();
                    //刷新表
                    int zt = Convert.ToInt32(MeiJu.JR);
                    DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT='{1}'", GeRenXX.Id, zt));
                    this.dataGridView1.DataSource = null;
                    this.dataGridView1.DataSource = table;
                }
                else
                {
                    MessageBox.Show("下单失败！");
                }

            }
            else
            {
                MessageBox.Show("结算失败,未选中...");
            }
            
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            int ZT = Convert.ToInt32(MeiJu.JR);
            DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT='{1}'", GeRenXX.Id, ZT));
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = table;

            DataTable Table = DBHelper.select(string.Format("select * from T_YongHu where YHId='{0}'", GeRenXX.Id));
            int money = Convert.ToInt32(Table.Rows[0]["YuE"]);
            this.label6.Text = money.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DiCi.ChongZhi from = new ChongZhi();
            from.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DiCi.CaiPin from = new CaiPin();
            from.Show();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //订单号
            string DDH=this.dataGridView1.Rows[0].Cells["Col_DDH"].Value.ToString();
            //用户Id
            int Id=GeRenXX.Id;
            int sr = DBHelper.ZSG(string.Format("delete from T_DingDan where YHId='{0}' and DDH='{1}'",Id,DDH));
            if (sr>0)
            {
                MessageBox.Show("删除成功！");
                //刷新
                int ZT = Convert.ToInt32(MeiJu.JR);
                DataTable table = DBHelper.select(string.Format("select * from T_DingDan where YHId='{0}' and DDZT='{1}'", GeRenXX.Id, ZT));
                this.dataGridView1.DataSource = null;
                this.dataGridView1.DataSource = table;
            }
            else
            {
                MessageBox.Show("删除失败...");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DDGL.DD_SeleDele from = new DDGL.DD_SeleDele();
            from.Show();
        }

        private void text_Name_TextChanged(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            //合计费用
            int Money = 0;
            for (int i = 0; i < this.dataGridView1.SelectedRows.Count; i++)
            {
                //价格
                Money += Convert.ToInt32(this.dataGridView1.SelectedRows[i].Cells["Col_JG"].Value);
            }
            this.HeJi.Text = Money.ToString();
        }
    }
}
